Skip to content

tests: replace many .unwrap()s with ?s using eyre#9111

Draft
glehmann wants to merge 7 commits intojj-vcs:mainfrom
glehmann:gln/refactor-unwrap-to-question-mark-in-tests-lqyw
Draft

tests: replace many .unwrap()s with ?s using eyre#9111
glehmann wants to merge 7 commits intojj-vcs:mainfrom
glehmann:gln/refactor-unwrap-to-question-mark-in-tests-lqyw

Conversation

@glehmann
Copy link
Contributor

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, demos/)
  • I have updated the config schema (cli/src/config-schema.json)
  • I have added/updated tests to cover my changes
  • I fully understand the code that I am submitting (what it does,
    how it works, how it's organized), including any code drafted by an LLM.
  • For any prose generated by an LLM, I have proof-read and copy-edited with
    an eye towards deleting anything that is irrelevant, clarifying anything
    that is confusing, and adding details that are relevant. This includes,
    for example, commit descriptions, PR descriptions, and code comments.

glehmann and others added 4 commits March 14, 2026 17:51
…Result from parent commit

This commit should be comparable with jj-vcs#9050

As of this writing, this replaced all but out of 1053 instances of
`.block().unwrap()`. The remaining ones (as counted by AI):

```

┌──────────────────┬───────┬────────────────────────────────────────────────────┐
  │     Category     │ Count │                   Could convert?
│

├──────────────────┼───────┼────────────────────────────────────────────────────┤
  │ testutils        │ 20    │ No — return concrete types, would cascade to
│
  │ helpers          │       │ hundreds of callers across all test files
│

├──────────────────┼───────┼────────────────────────────────────────────────────┤
  │ Closures         │ 53    │ No — closures return () or concrete values
│

├──────────────────┼───────┼────────────────────────────────────────────────────┤
  │ Test helpers     │ 22    │ Technically yes, but adds ~70 ? at call sites
to   │
  │                  │       │ save 22 unwraps
│

├──────────────────┼───────┼────────────────────────────────────────────────────┤
  │ Non-convertible  │ 4     │ No — CommandError (2), Pin<Box<dyn Future>>
(1),   │
  │                  │       │ closure in test_matrix (1)
│

└──────────────────┴───────┴────────────────────────────────────────────────────┘
```
@glehmann glehmann changed the title Gln/refactor unwrap to question mark in tests lqyw tests: replace many .unwrap()s with ?s using eyre Mar 14, 2026
@ilyagr
Copy link
Contributor

ilyagr commented Mar 15, 2026

Thanks for looking at this!

Have you tested what the backtraces look like? My understanding is that colored-eyre's backtraces will look like the version of panic backtraces you get with RUST_BACKTRACE=full, which are noticeably more verbose and confusing than the backtraces you get with RUST_BACKTRACE=1. There are examples in the pr message of my pr.

That's the issue that turned that pr into a bit of a headache.

@ilyagr
Copy link
Contributor

ilyagr commented Mar 15, 2026

Another reason I was doubtful about this approach is the need to either "install" colored-eyre in every test or use the ctor crate. ctor's docs make it seem a bit questionable.

Also I'm not sure whether this installing affects just the panic handler, or whether it also allows backtraces when you use ?. The annoying thing is that eyre absolutely collects the backtraces on ?, it just doesn't print them in impl Debug, which is what all the test handles use to print the error.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following commits do not follow our format for subject lines:

  • b3eabf0: more unwrap() converted to ?
  • 445b3f9: error in simple_op_store.rs

Commits should have a subject line following the format <topic>: <description>. Please review the commit guidelines for more information.

@glehmann
Copy link
Contributor Author

color-eyre has a mechanism to filter the backtrace. I've implemented it in this PR.

Here is how it looks by default with cargo t simple_op_store::tests::test_read_write_view:

image

with RUST_BACKTRACE=full cargo t simple_op_store::tests::test_read_write_view

image

with COLORBT_SHOW_HIDDEN=1 cargo t simple_op_store::tests::test_read_write_view:

image

Installing color-eyre in each takes looked way too intrusive, but using ctor to avoid that seems acceptable to me.

color-eyre is also lacking the column number in the location—this is an easy fix in color-eyre though, and is probably acceptable to not have while it's being integrated in color-eyre.

Filter backtrace frames to show only project code (jj_lib and jj_cli),
removing noise from dependencies and standard library to make error
reports easier to read during testing.

Also enable backtrace capture by default (RUST_LIB_BACKTRACE=1) in tests
so backtraces are shown without requiring the environment variable.
… in tests

So backtraces are shown without requiring the environment variable.
@glehmann glehmann force-pushed the gln/refactor-unwrap-to-question-mark-in-tests-lqyw branch from 3771490 to 0e3b5ad Compare March 15, 2026 20:40
@github-actions github-actions bot dismissed their stale review March 15, 2026 20:40

All commits are now correctly formatted. Thank you for your contribution!

@glehmann
Copy link
Contributor Author

The message also looks nice with a panic:

image

The backtrace shows more frames than before because I ran the test with #[tokio::test].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants